/**
 *
 * @author jorge
 * 
 */package dao;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class conexionDAO {
    private static Connection conn;
    private static String url, contrasena, usuario, bd;
    private Statement stmt;
    private String driverClassName; 
    
    public conexionDAO(){
        //Parametros ecesarios para conectar con MySQL
        //porner el nombre de usuario, la contrasña, y la ruta del servidor
        usuario = "root";
        contrasena = "root";
        bd = "serviciosasistencia";
        url = "jdbc:mysql://localhost/" + bd;
        driverClassName = "com.mysql.jdbc.Driver";       
    }
        
 
    /************************METODO PARA ESTABLECER CONEXION *****************************/
    
    public boolean TestConnection()  {
        conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();     
            //System.out.println(url+usuario+contrasena);
            conn = DriverManager.getConnection(url, usuario, contrasena);
            //System.out.println(conn);
            if (conn != null) {
                conn.close();
                return true;
                }
        } catch (InstantiationException ex) {
            Logger.getLogger(conexionDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(conexionDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch(SQLException ex) {
            System.out.println("Hubo un problema al intentar conectarse con la base de datos "+url);
        }
        catch(ClassNotFoundException ex) {
            System.out.println(ex);
        }
        return false;       
    }

    /************************METODO PARA ABRIR CONEXION *****************************/

    public boolean abrirConexion() {
        if(TestConnection()){
            try {
                conn = DriverManager.getConnection(this.url, this.usuario, this.contrasena);
            } catch (SQLException ex) {
                Logger.getLogger(conexionDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            return true;
        }
            return false;
    
    }
    
    /************************METODO PARA CERRAR CONEXION *****************************/    
    
    public boolean cerrarConexion(){
        try{
            conn.close();
            return true;
        }
        catch(Exception ex){
            return false;
        }
    }
   
    /************************OBTENER PARAMETROS DE CONEXION *****************************/
   
    public Connection getConn() {
        return conn;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public String getPassBD() {
        return contrasena;
    }

    public String getUrl() {
        return url;
    }

    public String getUsrBD() {
        return usuario;
    }

    /************************METODO ESTABLECER PARAMETROS *****************************/
    
    public boolean setUsuario(String usuario){try{this.usuario = usuario; return true;}catch(Exception ex){return false;}}
    
    public boolean setContrasena(String contrasena){try{this.contrasena = contrasena; return true;}catch(Exception ex){return false;}}
    
    public boolean setURL(String url){try{this.url = url; return true;}catch(Exception ex){return false;}}
    
    public boolean setBD(String bd){try{this.bd = bd; return true;}catch(Exception ex){return false;}}
    
    /************************SENTENCIA SELECT *****************************/
    
    public ResultSet consulta(String query) throws SQLException{
        this.stmt = (Statement)conn.createStatement();
        return this.stmt.executeQuery(query);
    }
    
    /************************SENTENCIA UPDATE, ISERT, DELETE *****************************/
    
    public void execute(String query) throws SQLException{
        this.stmt = (Statement)conn.createStatement();
        stmt.executeUpdate(query);
    }
    
}


